package com.x.erp.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.x.erp.model.CarLog;
import com.x.framework.dao.BaseDao;

@Repository
public class CarLogDao extends BaseDao {

	private final static String selectCarLogListSql = "SELECT ecl.*,eu.USER_NAME FROM ERP_CAR_LOG ecl, ERP_USER eu WHERE ecl.USER_ID = eu.USER_ID ";

	/**
	 * 查询车辆日志记录列表
	 * 
	 * @param carLog
	 * @return
	 * @throws Exception
	 */
	public List<CarLog> selectCarLogList(CarLog carLog) throws Exception {
		StringBuilder sql = new StringBuilder(selectCarLogListSql);
		List<Object> params = new ArrayList<Object>();
		searchCondition(sql, params, carLog);
		sql.append("ORDER BY ecl.CAR_LOG_CREATE_TIME ASC ");
		return this.queryForList(sql.toString(), CarLog.class, params);
	}

	private void searchCondition(StringBuilder sql, List<Object> params, CarLog carLog) {
		if (carLog != null) {
			if (this.isString(carLog.getCarLogId())) {
				sql.append("AND ecl.CAR_LOG_ID = ? ");
				params.add(carLog.getCarLogId());
			}
			if (this.isString(carLog.getCarId())) {
				sql.append("AND ecl.CAR_ID = ? ");
				params.add(carLog.getCarId());
			}
			if (this.isString(carLog.getUserId())) {
				sql.append("AND ecl.USER_ID = ? ");
				params.add(carLog.getUserId());
			}
			if (this.isString(carLog.getCarStateKey())) {
				sql.append("AND ecl.CAR_STATE_KEY = ? ");
				params.add(carLog.getCarStateKey());
			}
			if (this.isNotNull(carLog.getCarStateValue())) {
				sql.append("AND ecl.CAR_STATE_VALUE = ? ");
				params.add(carLog.getCarStateValue());
			}
		}
	}

}
